package net.wofly.finance.expense.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import net.wofly.common.condition.Condition;
import net.wofly.common.condition.ConditionRelate;
import net.wofly.common.condition.ConditionUtil;
import net.wofly.common.condition.FilterCondition;
import net.wofly.common.condition.Operation;
import net.wofly.common.json.XiNiaoJSON;
import net.wofly.finance.expense.domain.ExpenseAccount;
import net.wofly.finance.expense.domain.ExpenseType;
import net.wofly.finance.expense.repository.ExpenseTypeRepository;
import net.wofly.finance.expense.service.IExpenseTypeService;
import net.wofly.framework.util.Identities;
import net.wofly.framework.util.ReturnBean;

@Service("iExpenseTypeService")
@Transactional
public class ExpenseTypeServiceImpl implements IExpenseTypeService {
	
	@Autowired
	ExpenseTypeRepository expenseTypeRepository;

	@Override
	public ReturnBean<List<ExpenseType>> getAllExpenseTypeList() {
		Condition condition = new Condition();
        List<FilterCondition> filterConditions = new ArrayList<FilterCondition>();
        
    		filterConditions.add(new FilterCondition("1", "status", ConditionRelate.and.name(), Operation.eq.name(), 1)) ;
        
        condition.setFilterCondition(filterConditions);
        
        condition = ConditionUtil.buildCondition(XiNiaoJSON.toJSONString(condition), ExpenseType.class).getResult();
		
		Sort sort = new Sort(Sort.Direction.ASC, "sortNo");
		
		return new ReturnBean<>(ReturnBean.CODE_SUCCESS ,"获取报销单费用类型列表成功！" , expenseTypeRepository.findAll(condition.getSpecification() ,sort)) ;
	}

	@Override
	public ReturnBean<String> del(String expenseTypeID) {
		ExpenseType expenseType = expenseTypeRepository.findOne(expenseTypeID) ;
		if(null == expenseType) {
			return new ReturnBean<>(920110, "要删除的费用类型不存在") ;
		}
		
		expenseType.setStatus(0);
		expenseTypeRepository.save(expenseType) ;
		
		return new ReturnBean<>(ReturnBean.CODE_SUCCESS, "删除费用类型成功！") ;
	}

	@Override
	public ReturnBean<ExpenseType> save(ExpenseType expenseType) {
		expenseType.setExpenseTypeID(Identities.uuid2());
		expenseType.setStatus(1);
		expenseType.setModifyDate(System.currentTimeMillis());
		return  new ReturnBean<>(ReturnBean.CODE_SUCCESS, "保存费用类型成功！",expenseTypeRepository.save(expenseType)) ;
	}

	@Override
	public ReturnBean<ExpenseType> update(ExpenseType expenseType) {
		expenseType.setStatus(1);
		expenseType.setModifyDate(System.currentTimeMillis());
		return  new ReturnBean<>(ReturnBean.CODE_SUCCESS, "修改费用类型成功！",expenseTypeRepository.save(expenseType)) ;
	}

	@Override
	public ExpenseType findOne(String expenseTypeID) {
		return expenseTypeRepository.findOne(expenseTypeID);
	}


}
